package org.example.mrrope.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.dto.R;
import org.example.mrrope.service.IpadApiService;
import org.example.mrrope.utils.DingUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

@Api(value = "宜搭", tags = {"Ipad平板接口"})
@RestController
@RequestMapping("/ipad")
@Slf4j
public class IpadApiController {

    @Autowired
    IpadApiService ipadApiService;

    @Autowired
    DingUtil dingUtil;

    @GetMapping("/selectWorker")
    @ApiOperation("查当前工人进行中工单")
    public String selectWorker(@RequestParam("userName" ) String userName,
                               @RequestParam("workshop") String workshop,
                               @RequestParam("factoryType") String factoryType) {
        JSONArray ipadApi = null;
        try {
            ipadApi = ipadApiService.getIpadApi(userName, workshop, factoryType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ipadApi.toString();
    }

    @GetMapping("/selectWorkerXianQin")
    @ApiOperation("查询工单详情")
    public String selectWorkerXianQin(@RequestParam("objetId") String objetId) {
        String ipadApi = "";
        try {
            ipadApi = ipadApiService.selectWorkerXianQin(objetId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ipadApi;
    }

    @PostMapping("/insertWorkJiaoKu")
    @ApiOperation("新增工人缴库")
    public String insertWorkJiaoKu(@RequestBody HashMap<String,Object> map) {
        String ipadApi = "";
        try {
            String userName =  (String) map.get("userName");
            String workshop =  (String) map.get("workshop");
            String factory =  (String) map.get("factory");
            int number =  (int) map.get("number");
            String instanceId =  (String) map.get("instanceId");
            ipadApi = ipadApiService.inserWorkjiaoKu(userName, workshop,factory, number, instanceId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ipadApi;
    }

    @PostMapping("/getFjNotLoginUrl")
    @ApiOperation("获取图片")
    public String inserWorkjiaoKu(String url) {
        String returnUrl = null;
        try {
            returnUrl = dingUtil.getFjNotLoginUrl(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return returnUrl;
    }


//
//    //刷新作业表
//    @PostMapping("/shuaxinzy")
//    @ApiOperation("刷新作业")
//    public R shuaxinzy() {
//        try {
//            JSONObject searchJo = new JSONObject();
//            String s = dingUtil.yiDaSearchReturnData("FORM-66A0D556664246B69167368F08A4726BCMYJ", searchJo);
//            JSONArray bomOneJson = new JSONArray(s);
//            for (int i = 0; i < bomOneJson.length(); i++) {
//                JSONObject bomJsonObject = bomOneJson.getJSONObject(i).getJSONObject("formData");
//                String inId = bomOneJson.getJSONObject(i).getString("formInstanceId");
//                String instanceId = null;
//                //查询子表
//                if (bomJsonObject.has("associationFormField_m2co9jky")) {
//                    String substring = bomJsonObject.getString("associationFormField_m2co9jky").replaceAll("\\\\", "").substring(1);
//                    instanceId = new JSONArray(substring.substring(0, substring.length() - 1)).getJSONObject(0).getString("instanceId");
//                } else if (bomJsonObject.has("associationFormField_m2co9jky_id")) {
//                    String substring = bomJsonObject.getString("associationFormField_m2co9jky_id").replaceAll("\\\\", "").substring(1);
//                    instanceId = new JSONArray(substring.substring(0, substring.length() - 1)).getJSONObject(0).getString("instanceId");
//                }
//                if (instanceId != null) {
//                    //AC表
//                    String bomItem = null;
//                    bomItem = dingUtil.getYidaById(instanceId);
//                    //判断当前是否缴库
//                    JSONObject jsonObject = new JSONObject();
//                    if (bomItem != null){
//                        JSONObject bomItemJson = new JSONObject(bomItem);
//                        //箱数
//                        Integer xhs = bomItemJson.has("numberField_lwt597ij") ? bomItemJson.getInt("numberField_lwt597ij") : 0;
//                        if (bomItemJson.has("numberField_m5f5gz4w")){
//                            if(bomItemJson.getInt("numberField_m5f5gz4w") >= 0){
//                                jsonObject.put("numberField_m5f5gz4x",xhs);
//                            }else {
//                                jsonObject.put("numberField_m5f5gz4x",0);
//                            }
//                        }else {
//                            jsonObject.put("numberField_m5f5gz4x",xhs);
//                        }
//                        if (bomItemJson.has("numberField_m2ucxjgq")){
//                            if(bomItemJson.getInt("numberField_m2ucxjgq") >= 0){
//                                jsonObject.put("numberField_m5f5gz4v",xhs);
//                            }else {
//                                jsonObject.put("numberField_m5f5gz4v",0);
//                            }
//                        }else {
//                            jsonObject.put("numberField_m5f5gz4v",xhs);
//                        }
//                    }
//                    dingUtil.UpdateYiDa(instanceId, jsonObject);
//                }
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return R.ok("1");
//    }

}
